﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nhom14_HTTTHD_BuyMe.function
{
    public class BidWebsite
    {
        static public int AddBidWebsite(int id_account, int id_website, float price, string message)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    bid_website d = new bid_website();
                    d.id_account = id_account;
                    d.id_website = id_website;
                    d.message = message;
                    d.price = price;
                    d.timestamp = DateTime.Now;

                    context.bid_website.AddObject(d);
                    context.SaveChanges();
                    return d.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        static public float GetPrice(int bidID)
        {
            float price = 0;
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    //Use BidWebsite.GetPrice
                    var result = from x in context.bid_website
                                 where x.id == bidID
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    bid_website bid = result.First();

                    price = (float)(bid.price);
                    return price;
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int GetAccountBid(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.bid_website
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    bid_website bid = result.First();

                    return (int)bid.id_account;
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int GetWebsiteID(int id)
        {
            try
            {
                using(websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.bid_website
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    bid_website bid = result.First();

                    return (int)bid.id_website;
                }
            }
            catch
            {
                return 0;
            }
        }

        
    }
}